package com.yanqu.road.server.manger.assistant;

import com.yanqu.road.entity.assistant.AssistantBodySkin;
import com.yanqu.road.entity.assistant.UserAssistantDecoration;
import com.yanqu.road.entity.assistant.UserAssistantSuit;
import com.yanqu.road.entity.config.patrons.CombInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.logic.bussiness.player.UserAssistantSkinBusiness;
import com.yanqu.road.pb.assistant.AssistantSkinProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.assistant.AssistantSkinModule;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.AssistantSkinPb;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.yanqu.road.entity.enums.eItemId.*;

public class AssistantSkinMgr extends TempMgr {

    public static final Logger logger = LogManager.getLogger(AssistantSkinMgr.class);


    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }


    /**
     * 查看他人小玉数据
     */
    public static AssistantSkinProto.SeeOtherAssistantRespMsg.Builder getSeeAssistantMsg(long userId, int topNum) {
        AssistantSkinProto.SeeOtherAssistantRespMsg.Builder respMsg;
        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userId);
        AssistantBodySkin assistantBodySkin = null;
        List<UserAssistantDecoration> topDecoration = new ArrayList<>();
        if (onlinePlayer != null) {         //在线玩家
            AssistantSkinModule module = onlinePlayer.getModule(AssistantSkinModule.class);
            //拿出全身装扮和顶级装扮
            topDecoration = module.getTopDecorationList(topNum);
            assistantBodySkin = module.getAssistantBodySkin();
        } else {                            //离线玩家
            topDecoration = UserAssistantSkinBusiness.getTopDecoration(userId, topNum);
            assistantBodySkin = UserAssistantSkinBusiness.getUserAssistantBodySkin(userId);
        }
        //如果没拿到 传默认小助理
        if (null == assistantBodySkin) {
            assistantBodySkin = new AssistantBodySkin(0, ASSISTANT_DEFAULT_HEAD_DECORATION.getValue(), ASSISTANT_DEFAULT_FACE_DECORATION.getValue(), ASSISTANT_DEFAULT_CLOTHES_DECORATION.getValue(),
                    ASSISTANT_DEFAULT_PANTS_DECORATION.getValue(), ASSISTANT_DEFAULT_FOOT_DECORATION.getValue(), ASSISTANT_DEFAULT_HAND_DECORATION.getValue(), ASSISTANT_DEFAULT_OTHER_DECORATION.getValue(),
                    ASSISTANT_DEFAULT_SUIT.getValue());
        }
        respMsg = AssistantSkinPb.parseSeeOtherAssistantRespMsg(assistantBodySkin, topDecoration);
        respMsg.setRet(0);
        return respMsg;
    }

    /**
     * 获得小助理皮肤技能效果加成
     */
    public static int getSkinAddition(Map<Integer, UserAssistantDecoration> userAssistantDecorationMap, int skillType, int occupation) {
        int addition = 0;
        for (UserAssistantDecoration userAssistantDecoration : userAssistantDecorationMap.values()) {
            SkinInfo skinInfo = SkinMgr.getSkinInfo(userAssistantDecoration.getDecorationId());
            for (int effectSkill : skinInfo.getSkinEffect()) {
                if(effectSkill > 0) {
                    SkillInfo skillInfo = SkillMgr.getSkillInfo(effectSkill);
                    if (skillInfo.getType() == skillType && (skillInfo.getParamList().get(2) == 0 || skillInfo.getParamList().get(2) == occupation)) {
                        addition += SkillMgr.getSkillAddition(skillInfo, userAssistantDecoration.getDecorationLevel());
                    }
                }
            }
        }
        return addition;
    }

    /**
     * 获得小助理套装技能效果加成
     */
    public static long getSuitAddition(Map<Integer, UserAssistantSuit> userAssistantSuitMap, int skillType, int occupation) {
        long addition = 0;
        if (userAssistantSuitMap == null) {
            return addition;
        }
        for (UserAssistantSuit userAssistantSuit : userAssistantSuitMap.values()) {
            CombInfo combInfo = SkinMgr.getAssistantSkinCombMap().get(userAssistantSuit.getSuitId());
            if(combInfo.getCombSkillId() > 0) {
                SkillInfo skillInfo = SkillMgr.getSkillInfo(combInfo.getCombSkillId());
                if (skillInfo.getType() == skillType && (skillInfo.getParamList().get(2) == 0 || skillInfo.getParamList().get(2) == occupation)) {
                    addition += SkillMgr.getSkillAddition(skillInfo, userAssistantSuit.getSuitLevel());
                }
            }
        }
        return addition;
    }


    public static CombInfo getAssistantSuitCombInfo(int combId) {
        CombInfo combInfo = SkinMgr.getAssistantSkinCombMap().get(combId);
        if (combInfo == null) {
            return null;
        }
        return combInfo;
    }
}
